package com.videri.match.server;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import com.videri.match.client.Head2HeadService;
import javax.servlet.*;

public class Head2HeadServlet extends RemoteServiceServlet implements
		Head2HeadService {

	// This is the string associated with the waiting player number attribute
	private final String WAITING_PLAYER_NUM = "Head to head waiting player";
	
	private boolean otherWaiting;
	private Integer waitingPlayerNum;
	
	public int checkForWaitingPlayer(int playerNum) {
		if(waitingPlayerNum==null){
			otherWaiting = true;
			waitingPlayerNum = new Integer(-1);
			placeInContext();
			return -1;
		}
		
		if(otherWaiting){
			otherWaiting=false;
			waitingPlayerNum = new Integer(playerNum);
			placeInContext();
			return playerNum;
		}
		else{
			otherWaiting=true;
			waitingPlayerNum = new Integer(-1);
			placeInContext();
			return -1;
		}
	}

	private void placeInContext(){
		ServletContext con = this.getServletContext();
		con.setAttribute(WAITING_PLAYER_NUM, waitingPlayerNum);
	}
}
